﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clipper.Model;
using System.Threading;
using Global;
using System.Windows;
using System.Collections.ObjectModel;

namespace Clipper.BLL.DetectChain
{
    /// <summary>
    /// 恒温等待
    /// </summary>
    public class WaitReaction:ApplianceComponent
    {
        #region 变量和属性
        /// <summary>
        /// 执行下一步信号量
        /// </summary>
        static AutoResetEvent mCompleteReset = new AutoResetEvent(false);
        static ControlStatusEnum detectionStatus;
        /// <summary>
        /// 状态
        /// </summary>
        public override ControlStatusEnum DetectionStatus
        {
            get
            {
                if (ServiceProvider.Monitor.WaitReactionStrips.Count >= ServiceProvider.MaxWaitReactionCount)//未达最大数量
                {
                    detectionStatus = ControlStatusEnum.BUSY;
                }
                else
                {
                    detectionStatus = ControlStatusEnum.IDLE;
                }
                return detectionStatus;
            }
            set
            {
                detectionStatus = value;
            }
        }
        static AutoResetEvent mPause = new AutoResetEvent(false);//暂停
        static bool mIsPause;
        /// <summary>
        /// 获得或设置模块暂停状态
        /// </summary>
        public static bool IsPause
        {
            get
            {
                return mIsPause;
            }
        }
        static ObservableCollection<byte> seatList = null;
        /// <summary>
        /// 空位置列表
        /// </summary>
        public static ObservableCollection<byte> SeatList
        {
            get
            {
                if (seatList == null)
                {
                    seatList = new ObservableCollection<byte>();
                    byte _position = 70;//硬件位置
                    for (byte i = 1; i <= ServiceProvider.MaxWaitReactionCount; i++)
                    {
                        seatList.Add(_position);
                        _position--;
                    }
                }
                return seatList;
            }
            set
            {
                seatList = value;
            }
        }
        #endregion

        public override void Run(ReactionCup strip)
        {
            //if (ServiceProvider.Monitor.IncubationBoxReactionStrips.Count == ServiceProvider.MaxWaitReactionCount)//到达最大数量
            //{
            //    detectionStatus = ControlStatusEnum.BUSY;
            //}
            //Monitor.Enter(ServiceProvider.Monitor);
            //if (Application.Current != null)
            //{
            //    Application.Current.Dispatcher.Invoke(new Action(() =>
            //    {
            //        ServiceProvider.Monitor.WaitReactionStrips.Add(strip);//添加到检测仓
            //    }));
            //}
            //Monitor.Exit(ServiceProvider.Monitor);
            //strip.ReactionStripSeat = ReactionStripStageEnum.WAITREACTION;
            //strip.Note = this.GetType().Name + Thread.CurrentThread.Name + "--" + strip.ID;
            //Console.WriteLine(strip.Note);

            //if (ServiceProvider.IsSimulate)
            //{
            //    Simulate(strip);
            //}
            //else
            //{
            //    //strip.LightDetectEnd = DateTime.Now.AddMinutes(strip.WhileLightReact);//实际
            //    strip.LightDetectEnd = DateTime.Now.AddMinutes(7);
            //    TimeSpan timeSpan = strip.LightDetectEnd - DateTime.Now;
            //    Thread.Sleep(timeSpan);     //持续时间孵育 
            //}
            //Transportation.AddTransportation(strip, this, NextComponent);
        }
        /// <summary>
        /// 暂停/恢复
        /// </summary>
        /// <param name="isPause">暂停true,恢复false</param>
        public static void Pause(bool isPause)
        {
            mIsPause = isPause;
            
        }
        public override void Simulate(ReactionCup strip)
        {
            //strip.LightDetectEnd = DateTime.Now.AddMinutes(strip.WhileLightReact);
            //int sleep = int.Parse((strip.WhileLightReact * 1000 * ServiceProvider.ForwardTimes).ToString());
            //Thread.Sleep(sleep);     //持续时间孵育
            //mCompleteReset.WaitOne();
        }

        /// <summary>
        /// 完成信号
        /// </summary>
        public static void CompleteSingle()
        {
            mCompleteReset.Set();
        }
    }
}
